package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfMerchPayInfo;
import com.ocom.common.entity.card.EntityXfMerchantInfo;
import com.ocom.common.entity.typehandler.EntityCcbBusinessInfo;
import com.ocom.common.entity.typehandler.EntityCcbPosInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.request.payconfig.UpdateTollPointInfoRequest;
import com.ocom.common.request.payhandle.GetCcbDeleteBusinessRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.response.CcbBusinessInfoResponse;
import com.ocom.common.response.DeviceCcbPosListResponse;
import com.ocom.common.utils.ResultUtil;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoCcbBusinessInfo;
import com.ocom.payconfig.dao.IDaoXfDeviceInfo;
import com.ocom.payconfig.service.CcbBusinessInfoService;
import com.ocom.payconfig.service.CcbPosInfoService;
import com.ocom.payconfig.service.XfMerchantInfoService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 智慧生活商家信息 服务实现类
 * </p>
 *
 * @author js
 * @since 2023-10-04
 */
@Service
public class CcbBusinessInfoServiceImpl extends ServiceImpl<IDaoCcbBusinessInfo, EntityCcbBusinessInfo> implements CcbBusinessInfoService {
    @Resource
    private CcbPosInfoService ccbPosInfoService;
    @Resource
    private IDaoCcbBusinessInfo iDaoCcbBusinessInfo;
    @Resource
    private XfMerchantInfoService xfMerchantInfoService;
    @Resource
    private IDaoXfDeviceInfo xfDeviceInfo;
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public Result getCcbDeleteBusiness(Long comId,GetCcbDeleteBusinessRequest request){
        QueryWrapper<EntityCcbPosInfo> queryPos = Wrappers.query();
        queryPos.select("device_no");
        queryPos.eq("com_id",request.getComId());
        queryPos.eq("business_id",request.getBusinessId());
        queryPos.ne("device_no","");
        queryPos.last("limit 1");
        EntityCcbPosInfo posInfo = ccbPosInfoService.getOne(queryPos);
        if(posInfo != null){
            return new Result(501,"商家已绑定设备，不能移除。", null);
        }

        UpdateWrapper<EntityCcbPosInfo> updatePos = Wrappers.update();
        updatePos.eq("com_id",request.getComId());
        updatePos.eq("business_id",request.getBusinessId());
        ccbPosInfoService.remove(updatePos);

        UpdateWrapper<EntityCcbBusinessInfo> updateBusiness = Wrappers.update();
        updateBusiness.eq("com_id",request.getComId());
        updateBusiness.eq("business_id",request.getBusinessId());
        boolean bRemove = this.remove(updateBusiness);
        return ResultUtil.success(bRemove);
    }

    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public Result getCcbBusinessList(Long comId,GetCcbDeleteBusinessRequest request){
        List<CcbBusinessInfoResponse> list = iDaoCcbBusinessInfo.getCcbBusinessList(request.getComId());
        return ResultUtil.success(list);
    }
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId")
    public Boolean updateDeviceByCcb(Long comId,UpdateTollPointInfoRequest request){
        QueryWrapper<EntityCcbBusinessInfo> queryCcbBusiness = Wrappers.query();
        queryCcbBusiness.select("com_id","merch_id");
        queryCcbBusiness.eq("com_id",request.getCom_id());
        queryCcbBusiness.eq("merch_id",request.getMerch_id());
        List<EntityCcbBusinessInfo> listBusiness = this.list(queryCcbBusiness);
        if(listBusiness == null || listBusiness.size()==0){
            return true;
        }
        EntityXfMerchantInfo merchPayInfo = xfMerchantInfoService.GetMerchInfoById(request.getCom_id(),request.getMerch_id());
        if(merchPayInfo == null){
            return true;
        }
        List<DeviceCcbPosListResponse> getDeviceCcbPos = xfDeviceInfo.getDeviceccbPos(request.getCom_id(),request.getPoint_id());
        if(getDeviceCcbPos == null || getDeviceCcbPos.size() == 0){
            return true;
        }
        String merchOrigin = merchPayInfo.getMerchOrgin();
        String[] strOrigins = null;
        if(merchOrigin.contains(",")){
            strOrigins = merchOrigin.split(",");
        }else if(merchOrigin != null && !merchOrigin.equals("")){
            strOrigins[0] = merchOrigin;
        }
        List<String> listOrigin = new ArrayList<>(strOrigins.length);
        listOrigin.add(String.valueOf(request.getMerch_id()));
        Collections.addAll(listOrigin, strOrigins);
        listOrigin.add("0");
        for(EntityCcbBusinessInfo businessInfo : listBusiness){
            for(String strOrigin : listOrigin){
                if(businessInfo.getMerchId().equals(Long.valueOf(strOrigin)) ){
                    for(DeviceCcbPosListResponse deviceCcbPos : getDeviceCcbPos){
                        if(! deviceCcbPos.getBindMerchId().equals(businessInfo.getMerchId())){
                            ccbPosInfoService.updateByDeviceNo(request.getCom_id(),deviceCcbPos.getDeviceNo());
                        }
                    }
                }
            }
        }
        return true;
    }

}
